<template>
  <c-form-dialog
    ref="form"
    :visible.sync="innerVisible"
    :form-items="formItems"
    :form-data="formData"
    :table-name="tableName"
    :table-id="tableId"
    @close="handleClose"
    @success="handleSuccess"
    :before-save="handleBeforeSave"
  ></c-form-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import SelectOrg from '@/credit/views/county/settle/capital/component/SelectOrg'
export default {
  components: {},
  props: {
    visible: Boolean,
    tableId: String,
    formData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    return {
      innerVisible: false,
      tableName: 'capital_pool_register',
      formItems: [
        {
          tag: SelectOrg,
          itemAttrs: {
            label: '机构名称',
            rules: [{ required: true, message: '请选择/填写机构名称', trigger: 'change' }],
          },
          attrs: {
            class: 'form-control-ml',
            key: 'agency_name',
            placeholder: '请选择/填写机构名称',
          },
          events: {
            select: (model, item) => {
              model.agency_id = item?.agency_id ?? ''
            },
          },
        },
        {
          tag: 'input',
          attrs: {
            key: 'agency_id',
          },
          ifShow: false,
        },
        {
          tag: 'date',
          itemAttrs: {
            label: '合作开始时间',
            rules: [{ required: true, message: '请选择合作开始时间', trigger: 'change' }],
          },
          attrs: { key: 'coo_start_date', value: '', placeholder: '请选择合作开始时间' },
        },
        {
          tag: 'date',
          itemAttrs: {
            label: '合作结束时间',
            rules: [{ required: true, message: '请选择合作结束时间', trigger: 'change' }],
          },
          attrs: { key: 'coo_end_date', value: '', placeholder: '请选择合作结束时间' },
        },
        {
          tag: 'date',
          itemAttrs: {
            label: '资金池支付时间',
            rules: [{ required: true, message: '请输入资金池支付时间', trigger: 'change' }],
          },
          attrs: { key: 'pay_time', value: '', placeholder: '请输入资金池支付时间' },
        },

        {
          tag: 'number',
          itemAttrs: {
            label: '资金池金额',
            rules: [{ required: true, message: '请输入资金池金额', trigger: 'blur' }],
          },
          attrs: {
            key: 'capital_funds',
            placeholder: '请输入资金池金额',
          },
          tagSlots: [{ name: 'append', slot: '万元' }],
          events: {
            input: (model, v) => {
              model.coo_funds = Math.round((v ?? 0) * (model?.multiple ?? 0) * 100) / 100
            },
          },
        },

        {
          tag: 'number',
          itemAttrs: {
            label: '放大倍数',
            rules: [{ required: true, message: '请输入放大倍数', trigger: 'blur' }],
          },
          attrs: {
            key: 'multiple',
            placeholder: '请输入放大倍数',
          },
          events: {
            input: (model, v) => {
              model.coo_funds = Math.round((v ?? 0) * (model?.capital_funds ?? 0) * 100) / 100
            },
          },
        },
        {
          tag: 'number',
          itemAttrs: {
            label: '合作金额',
            rules: [{ required: true, message: '请输入合作金额', trigger: 'blur' }],
          },
          attrs: {
            key: 'coo_funds',
            placeholder: '请输入合作金额',
            disabled: true,
          },
          tagSlots: [{ name: 'append', slot: '万元' }],
        },
        {
          tag: 'textarea',
          itemAttrs: {
            label: '备注',
          },
          attrs: {
            key: 'remark',
            value: '',
            autosize: { minRows: 3 },
            placeholder: '备注',
          },
        },
        {
          tag: 'upload',
          itemAttrs: {
            label: '附件',
          },
          attrs: {
            class: 'form-control-ml',
            key: 'attachment',
            value: '',
            placeholder: '附件',
            accept: '',
            fileName: 0,
            listType: '',
            dir: `/credit/settle/capital/${this.tableId || +new Date()}`,
          },
        },
      ],
    }
  },
  computed: {
    ...mapGetters(['user', 'clientId']),
  },
  watch: {
    visible(val) {
      this.innerVisible = val
    },
  },
  created() {},
  mounted() {},
  methods: {
    handleBeforeSave(model) {
      model.service_id = this.clientId
      model.submit_person_name = this.user.realName
      return model
    },
    handleClose() {
      this.$emit('update:visible', false)
    },
    handleSuccess(res) {
      this.$emit('success', res)
    },
  },
}
</script>
<style lang="scss" scoped></style>
